import { intersection, isEmpty } from 'lodash';

export default function access(initialState: { currentUser?: AircasKB.User } | undefined) {
    const { currentUser } = initialState ?? {};
    let hasAuth = (route: any, roles?: AircasKB.Role[]) => {
        if (isEmpty(route.roles)) {
            return true;
        }
        return !isEmpty(
            intersection(
                roles?.map((item) => item.name),
                route.roles,
            ),
        );
    };
    return {
        canAdmin: () => {
            return true;
            //TODO 后期修改权限校验规则
            // return (currentUser && currentUser.roleNames?.includes('admin')) ?? false;
        },
        auth: (route: any) => hasAuth(route, currentUser?.roles ?? []),
    };
}
